﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LIMS.SqlServerDAL
{
    /// <summary>
    /// 学生服务类
    /// By：田玉粮
    /// 2013.3.19
    /// </summary>
    public class StudentService : LIMS.IDAL.IStudent
    {
        LIMSDataContext DB = new LIMSDataContext();

        /// <summary>
        /// 添加单个学生数据
        /// </summary>
        /// <param name="student"></param>
        /// <returns></returns>
        public void Add(Model.Students stu)
        {
            TB_Student student = new TB_Student()
            {
                stuNo = stu.stuNo,
                stuPwd = stu.stuPwd,
                stuGender = stu.stuGender,
                stuName = stu.stuName,
                classID = stu.classID
            };
            DB.TB_Student.InsertOnSubmit(student);
            DB.SubmitChanges();
        }

        /// <summary>
        /// 查找所有的学生数据
        /// </summary>
        /// <returns></returns>
        public List<Model.Students> FindAll()
        {
            List<Model.Students> listStu = new List<Model.Students>();
            DB.TB_Student.Select(s => s).ToList().ForEach((item) =>
            {
                Model.Students stuTemp = new Model.Students()
                {
                    stuNo = item.stuNo,
                    stuPwd = item.stuPwd,
                    stuName = item.stuName,
                    stuGender = item.stuGender,
                    classID = item.classID
                };
                listStu.Add(stuTemp);
            });
            return listStu;
        }

        /// <summary>
        /// 根据ID查找学生数据
        /// </summary>
        /// <param name="stu"></param>
        /// <returns></returns>
        public List<Model.Students> FindByID(Model.Students stu)
        {
            List<Model.Students> listStu = new List<Model.Students>();
            DB.TB_Student.Where(w => w.stuNo.StartsWith(stu.stuNo)).Select(s => s).ToList().ForEach((student) =>
            {
                Model.Students stuTemp = new Model.Students()
                {
                    stuNo = student.stuNo,
                    stuPwd = student.stuPwd,
                    stuName = student.stuName,
                    stuGender = student.stuGender,
                    classID = student.classID
                };
                listStu.Add(stuTemp);
            });
            return listStu;
        }

        /// <summary>
        /// 根据姓名查找学生数据
        /// </summary>
        /// <param name="stu"></param>
        /// <returns></returns>
        public List<Model.Students> FindByName(Model.Students stu)
        {
            List<Model.Students> listStu = new List<Model.Students>();
            DB.TB_Student.Where(w => w.stuName.Contains(stu.stuName)).Select(s => s).ToList().ForEach((student) =>
            {
                Model.Students stuTemp = new Model.Students()
                {
                    stuNo = student.stuNo,
                    stuPwd = student.stuPwd,
                    stuName = student.stuName,
                    stuGender = student.stuGender,
                    classID = student.classID
                };
                listStu.Add(stuTemp);
            });
            return listStu;
        }

        /// <summary>
        /// 删除单个学生数据
        /// </summary>
        /// <param name="stu"></param>
        /// <returns></returns>
        public Boolean Remove(Model.Students stu)
        {
            var student = DB.TB_Student.SingleOrDefault(s => s.stuNo == stu.stuNo);
            DB.TB_Student.DeleteOnSubmit(student);
            DB.SubmitChanges();
            return true;
        }

        /// <summary>
        /// 删除多个学生数据
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public Boolean Remove(List<Model.Students> list)
        {
            List<TB_Student> listTemp = new List<TB_Student>();
            list.ForEach((item) =>
            {
                TB_Student student = new TB_Student()
                {
                    stuNo = item.stuNo,
                    stuName = item.stuName,
                    stuPwd = item.stuPwd,
                    stuGender = item.stuGender,
                    classID = item.classID
                };
                listTemp.Add(student);
            });
            DB.TB_Student.DeleteAllOnSubmit(listTemp);
            DB.SubmitChanges();
            return true;
        }

        /// <summary>
        /// 更新单个学生信息
        /// </summary>
        /// <param name="stu"></param>
        /// <returns></returns> 
        public Boolean Modify(LIMS.Model.Students stu)
        {
            var stuTmp = DB.TB_Student.SingleOrDefault(s => s.stuNo == stu.stuNo);
            stuTmp.stuName = stu.stuName;
            stuTmp.stuGender = stu.stuGender;
            stuTmp.stuPwd = stu.stuPwd;
            stu.classID = stu.classID;
            DB.SubmitChanges();
            return true;
        }

    }
}
